﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading;

namespace Utils
{
    public static class RabbitMqStatu
    {
        /// <summary>
        /// 路由名称
        /// </summary>
        public static string ExchangeName;
        /// <summary>
        /// 队列名称
        /// </summary>
        public static string QueueName;
        /// <summary>
        /// 消费者key
        /// </summary>
        public static string RouteKey;

        /// <summary>
        /// MQ连接
        /// </summary>
        /// <returns></returns>
        public static ConnectionFactory Ractory
        {
            get
            {
                ConnectionFactory connection = new ConnectionFactory
                {
                    UserName = "chenjie",//用户名
                    Password = "123123",//密码
                    HostName = "localhost"//rabbitmq ip
                };
                return connection;
            }
        }

        /// <summary>
        /// 生产者发送消息
        /// </summary>
        /// <param name="Message">消息</param>
        /// <param name="SendUserId">发送人id</param>
        /// <returns></returns>
        public static ReceiveStatus SendMsg(string Message,int SendUserId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                //创建连接
                var connection = Ractory.CreateConnection();
                //创建通道
                var channel = connection.CreateModel();
                //声明一个队列  第二个参数为true 为消息持久化设置
                channel.QueueDeclare("InputMsg", false, false, false, null);
                //转义消息
                var sendBytes = Encoding.UTF8.GetBytes(Message);
                //发布消息
                channel.BasicPublish("", "InputMsg", null, sendBytes);
                //关闭连接
                channel.Close();
                connection.Close();
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "错误消息:" + ex.Message; 
            }
            return receiveStatus;
        }

        /// <summary>
        /// 消费者接收消息
        /// </summary>
        /// <param name="ReceiveName"></param>
        /// <returns></returns>
        public static ReceiveStatus ReceiveMag(string ReceiveName)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                //创建连接
                var connection = Ractory.CreateConnection();
                //创建通道
                var channel = connection.CreateModel();
                //事件基本消费者
                EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
                //接收到消息事件
                consumer.Received += (ch, ea) =>
                {
                    var message = Encoding.UTF8.GetString(ea.Body);
                    //延迟10秒接收
                    //Thread.Sleep(10000);
                    //确认该消息已被消费
                    channel.BasicAck(ea.DeliveryTag, false);
                };
                //启动消费者 设置为手动应答消息 //false为手动应答，true为自动应答
                channel.BasicConsume("InputMsg", false, consumer);
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "错误消息:" + ex.Message;
            }
            return receiveStatus;
        }
    }
}
